from list_node import ListNode

class Solution:
  def FindKthToTail(self , pHead: ListNode, k: int) -> ListNode:
    if not pHead or k == 0:
       return None
    last = pHead
    for i in range(k-1):
        if last:
            last = last.next
        else:
            return None
    if not last:
        return None
    res = pHead
    while last and last.next:
        last = last.next
        res = res.next
    return res
    '''
    if head == None or k == 0:
            return None
        phead = head
        pbehind = head
        for i in range(k-1):
            if phead.next == None:
                return None
            else:
                phead = phead.next
        while phead.next != None:
            phead = phead.next
            pbehind = pbehind.next
        return pbehind
    '''

  '''
   l = []
    while head:
        l.append(head)
        head = head.next
    if len(l) < k or k < 1:
        return None
    return l[-k]
  '''
if __name__ == '__main__':
     tail = ListNode(3)
     mid = ListNode(2)
     head = ListNode(1)
     head.next = mid
     mid.next = tail
     print(Solution().FindKthToTail(head, 2))
